<!--公用组件：鼠标移入展示更多元素
    /**
    * @example 调用示例
    *  <HoverShow">
    *    <header>Header</header>
    *    <main>main</main>
    *  </HoverShow>
    */
-->
<template>
    <div class="hover-show">
        <header @mouseenter="handleMouseEnter" @mouseleave="handleMouseLeave">
            <slot name="header">header</slot>
        </header>
        <main v-show="active"  @mouseenter="handleMouseEnter" @mouseleave="handleMouseLeave">
            <slot name="main">main</slot>
        </main>
    </div>
</template>

<script>
export default {
    name: 'HoverShow',
    data(){
        return {
            active: false,
            timer: null,
        }
    },
    methods: {
        handleMouseEnter() {
            if (this.timer) clearTimeout(this.timer);
            this.timer = setTimeout( () => {
                return this.show();
            }, 100);
        },
        handleMouseLeave() {
            if (this.timer) clearTimeout(this.timer);
            this.timer = setTimeout(() => {
                return this.hide();
            }, 150);
        },
        show() {
            this.active = true;
            this.$emit('open');
        },
        hide() {
            this.active = false;
            this.$emit('close');
        }
    }
}
</script>

<style>
    .hover-show {
        position: relative;
        display: flex;
        width: 50px;
        text-align: center;
        margin-right: 100px;
    }
    .hover-show main {
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        top: 150%;
        width: 50px;
        height: 50px;
        background: rgba(0,0,0, .3);
    }
</style>